package com.baichang.taoshunda.dao.impl;

import com.baichang.framework.dao.impl.BaseDaoImpl;
import com.baichang.taoshunda.dao.RyFriendDao;
import com.baichang.taoshunda.entity.RyFriend;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * Created by ${wushenglu} on 2018/5/22.
 */
@Repository
public class RyFriendDaoImpl extends BaseDaoImpl<RyFriend> implements RyFriendDao {

    @Override
    public int agree(String userId, String friendId, Integer status) {
        String sql = "UPDATE t_ry_friend SET `status`=? WHERE (user_id=? AND friend_id=?) OR (user_id=? AND friend_id=?)";
        return update(sql, status, userId, friendId, friendId, userId);
    }

    @Override
    public boolean setDisplayName(String friendId, String displayName, String userId) {
        String sql = "UPDATE t_ry_friend SET display_name=? WHERE  friend_id=? and user_id=?";
        return update(sql, displayName, friendId, userId) > 0;
    }

    @Override
    public List<Map<String, Object>> all(String userId, Integer nowPage, Integer pageSize) {
        String sql = "SELECT * FROM t_ry_friend WHERE user_id=? and delete_status=1 ORDER BY id DESC";
        return listForMapList(sql, nowPage, pageSize, userId);
    }

    /**
     * 10: 请求, 11: 被请求, 20: 同意, 21: 忽略, 30: 被删除
     *
     * @param friendId
     * @param userId
     * @param i
     * @return
     */
    @Override
    public Map<String, Object> profile(String friendId, String userId, int i) {
        // and delete_status=1
        String sql = "SELECT * FROM t_ry_friend WHERE friend_id=? AND user_id=? and status in (10,11,20) ORDER BY id DESC";
        return queryForMap(sql, friendId, userId);
    }

    /**
     * 10: 请求, 11: 被请求, 20: 同意, 21: 忽略, 30: 被删除
     *
     * @param userId
     * @param nowPage
     * @param pageSize
     * @param friendShipStatus
     * @return
     */
    @Override
    public List<Map<String, Object>> inviteAllList(String userId, Integer nowPage, Integer pageSize, Integer friendShipStatus) {
        String sql = "SELECT * FROM t_ry_friend WHERE user_id=? ";
        if (friendShipStatus == 0) {
            sql += " and status in(11,20,21) and delete_status=1";
            return listForMapList(sql, nowPage, pageSize, userId);
        } else if (friendShipStatus == 11) {
            sql += " and status=11 and delete_status=1";
            return listForMapList(sql, nowPage, pageSize, userId);
        } else {
            sql += " and status =?";
        }
        sql += " ORDER BY id DESC";
        return listForMapList(sql, nowPage, pageSize, userId, friendShipStatus);
    }

    @Override
    public void deleteByUserId(String userId, String friendId) {
        delete("friend_id=? AND user_id=?", friendId, userId);
    }

    @Override
    public List<Map<String, Object>> getListByAccount(String telPhone) {
        String sql = "SELECT user.* FROM ( SELECT IFNULL(us.id, bu.id) AS id , IFNULL(us.nick_name, bu.buss_name) AS `name` , IFNULL(us.head_pic, bu.head_pic) AS headPicpath , IFNULL(us.account, bu.account) AS account , IFNULL(us.ry_token, bu.ry_token) AS ryToken , CASE ISNULL(us.id) WHEN 1 THEN 2 WHEN 0 THEN 1 END AS type FROM t_trends_user tu LEFT JOIN t_user us ON tu.user_id = us.id AND us.user_state = 1 LEFT JOIN t_bussiness bu ON tu.bus_id = bu.id AND bu.state = 1 ) user WHERE `user`.account = ? ";
        return queryForMapList(sql, telPhone);
    }

    @Override
    public boolean deleteHistory(Integer id) {
        String sql = "UPDATE `t_ry_friend` SET `delete_status`='0' WHERE (`id`=?)";
        return update(sql, id) > 0;
    }

    @Override
    public void updateHeadPic(String userId, String headPic, String sex, String name) {
        String sql = "UPDATE `t_ry_friend` SET `head_pic`=?,sex=?,nick_name=? WHERE friend_id=?";
        update(sql, headPic, sex, name, userId);
    }

    @Override
    public RyFriend getFriend(String userId, String selfUserId) {
        String sql = "select * FROM t_ry_friend WHERE user_id =? AND friend_id=? ORDER BY id DESC LIMIT 1";
        return queryForBean(sql, selfUserId, userId);
    }

    /**
     * 通过设置的备注   源用户的昵称  账号模糊查询
     *
     * @param userId 用户聊天编号
     * @param key    关键字
     * @return
     */
    @Override
    public List<Map<String, Object>> findFriend(String userId, String key) {
        String sql = "SELECT a.* FROM ( SELECT rf.display_name,rf.head_pic, rf.friend_id, rf.user_id, rf.trends_user_id ," +
                " IFNULL(u.account, b.account) AS account , IFNULL(u.nick_name, b.buss_name) AS nickName FROM  t_ry_friend rf " +
                "LEFT JOIN t_user u ON u.trends_user_id = rf.trends_user_id" +
                " LEFT JOIN t_bussiness b ON b.trends_user_id = rf.trends_user_id " +
                "WHERE rf.user_id = ? AND `status` = 20 ) a" +
                " WHERE (a.display_name LIKE '%" + key + "%' OR a.account LIKE '%" + key + "%' OR a.nickName LIKE '%" + key + "%')";
        return queryForBeanList(sql, userId);
    }
}
